﻿using System;
using System.Web.Security;
using FFIV.Lib.DataComponent;
using FFIV.Lib.DataComponent.AritcleDSTableAdapters;
using FFIV.Lib.Utilities;

namespace FFIV.Lib.BusinessRules
{
	internal class ArticlesRules
	{
		internal static int AddArticle(Guid authorId, string authorName, int categoryId, string title, string summary, string content, String tags, int viewCount, bool isDraft)
		{
			CategoriesUtility.DisableCache();
			using (QueriesTableAdapter adapter = new QueriesTableAdapter())
			{
				return Convert.ToInt32(adapter.AddArticle(authorId, authorName, categoryId, title, summary, content, tags, viewCount, isDraft));
			}
		}

		internal static AritcleDS.ArticlesDataTable GetArticleById(int articleId)
		{
			using (ArticlesTableAdapter adapter = new ArticlesTableAdapter())
			{
				return adapter.GetArticleById(articleId);
			}
		}

		internal static void UpdateArticleById(int articleId, string authorName, int categoryId, string title, string summary, string content, string tags, bool isDraft)
		{
			CategoriesUtility.DisableCache();
			using (QueriesTableAdapter adapter = new QueriesTableAdapter())
			{
				adapter.UpdateArticleById(articleId, authorName, categoryId, title, summary, content, tags, isDraft);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetArticleByCategoryId(int categoryId)
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetArticlesByCategoryId(categoryId);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetAllArticles()
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetAllArticles();
			}
		}

		internal static void DeleteArticleById(int articleId)
		{
			CategoriesUtility.DisableCache();
			using (QueriesTableAdapter adapter = new QueriesTableAdapter())
			{
				adapter.DeleteArticleById(articleId);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetArticleDetialsById(int articleId)
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetAtricleDetailsById(articleId);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetArtivleByTitle(string title)
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetArticleByTitle(title);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetLatestArticleByNumber()
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetLatestArticleByNumber();
			}
		}

		internal static void IncreaseArticles(int articleId)
		{
			using (QueriesTableAdapter adapter = new QueriesTableAdapter())
			{
				adapter.IncreaseArticleViewCount(articleId);
			}
		}

		internal static AritcleDS.ArticlesDetailDataTable GetArticlesByUserId(Guid userId)
		{
			using (ArticlesDetailTableAdapter adapter = new ArticlesDetailTableAdapter())
			{
				return adapter.GetArticlesByUserId(userId);
			}
		}

		internal static AritcleDS.ArticlesDataTable GetFavorArticles()
		{
			using (ArticlesTableAdapter adapter = new ArticlesTableAdapter())
			{
				return adapter.GetFavoraArticles();
			}
		}
	}
}
